package 曾世昌12_15;

import cn.itcast.xml.XmlUtils;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class Demo {
    static long time;

    public static void main(String[] args) throws Exception {
        System.out.println( "=====================网上商城=================" );
        System.out.println( "1：【注册】\n2：【登录】\n3：【忘记密码】\n4：【查看商品类别信息】\n" +
                "5：【查看商品信息】\n6：【根据商品类别id查询商品信息】\n7：【立即购买】" );
        switch (new Scanner( System.in ).nextInt()) {
            case 1:
                regrster();
                break;
            case 2:
                logrn();
                time = new Date().getTime();
                break;
            case 3:
                forget();
                break;
            case 4:
                showcategory();
                break;
            case 5:
                showproduct();
                break;
            case 6:
                showproductDetails();
                break;
            case 7:
                buy();
                break;
            case 8:
                cats();
                break;
        }
    }

    public static void regrster() {
        Scanner scanner = new Scanner( System.in );
        System.out.println( "欢迎注册" );
        System.out.println( "请输入用户名：" );
        String name = scanner.nextLine();
        String phonenum = "";
        String pass1 = "";
        String pass2;
        List<User> users = XmlUtils.readAll( "E:\\untitled5\\src\\rc\\demo12_13\\user.xml", User.class );
        String str = users.get( users.size() - 1 ).getId();
        for (User user : users) {
            int a = 0;
            if (user.getUsername().equals( name )) {
                a++;
            }
            if (a != 0) {
                System.out.println( "用户名已存在，请重新注册" );
                return;
            }
        }
        for (User user : users) {
            System.out.println( "请输入手机号：" );
            phonenum = scanner.nextLine();
            int b = 0;
            if (user.getPhone().equals( phonenum )) {
                b++;
                if (b != 0) {
                    System.out.println( "该手机号已被注册，请重新选择其他手机号" );
                    return;
                }
            }
            break;
        }
        System.out.println( "请输入密码：" );
        pass1 = scanner.nextLine();
        System.out.println( "请再次输入密码：" );
        pass2 = scanner.nextLine();
        if (!pass1.equals( pass2 )) {
            System.out.println( "两次密码不一致，请重新注册！" );
            return;
        }
        users.add( new User( String.valueOf( Integer.parseInt( str ) + 1 ), name, pass1, phonenum, "普通用户", 0.0 ) );
        XmlUtils.write( "E:\\untitled5\\src\\rc\\demo12_13\\user.xml", users, false );
        System.out.println( "注册成功！！！" );


    }

    public static boolean logrn() {
        System.out.println( "欢迎来到大数据赞助的商城" );
        Scanner scanner = new Scanner( System.in );
        System.out.println( "请输入用户名或手机号：" );
        String name = scanner.nextLine();
        System.out.println( "请输入密码：" );
        String pass = scanner.nextLine();
        List<User> users = XmlUtils.readAll( "E:\\untitled5\\src\\rc\\demo12_13\\user.xml", User.class );
        int a = 0;
        for (User user : users) {
            if (user.getUsername().equals( name ) || user.getPhone().equals( name )) {
                if (pass.equals( user.getPassword() )) {
                    a++;
                }
            }
        }
        if (a == 0) {
            System.out.println( "用户名、密码错误，请重新登录" );
            return false;
        } else {
            System.out.println( "密码正确，大数据商城欢迎您" );
        }
        return true;
    }

    public static void forget() {
        System.out.println( "忘记密码？别怕，我保护你" );
        Scanner scanner = new Scanner( System.in );
        System.out.println( "请输入用户名：" );
        String name = scanner.nextLine();
        List<User> users = XmlUtils.readAll( "E:\\untitled5\\src\\rc\\demo12_13\\user.xml", User.class );
        int a = 0;
        for (User user : users) {
            if (user.getUsername().equals( name )) {
                a++;
            }
        }
        if (a == 0) {
            System.out.println( "没有该用户，请注册..." );
            return;
        } else if (a != 0) {
            System.out.println( "请输入绑定的手机号" );
            String num = scanner.nextLine();
            int a1 = 0;
            for (User user : users) {
                if (user.getPhone().equals( num )) {
                    a1++;
                }
                if (a1 != 0) {
                    System.out.println( "请输入新密码！" );
                    String psw1 = scanner.nextLine();
                    System.out.println( "请再次输入密码！" );
                    String psw2 = scanner.nextLine();
                    if (psw1.equals( psw2 )) {
                        ArrayList<User> users1 = new ArrayList<>();
                        for (int i = 0; i < users.size(); i++) {
                            if (name.equals( users.get( i ).getUsername() )) {
                                users.get( i ).setPassword( psw1 );
                            }
                            users1.add( new User( users.get( i ).getId(), users.get( i ).getUsername(), users.get( i ).getPassword(), users.get( i ).getPhone(), users.get( i ).getRole(), users.get( i ).getMoney() ) );
                        }
                        XmlUtils.write( "E:\\untitled5\\src\\rc\\demo12_13\\user.xml", users1, false );
                        System.out.println( "修改密码成功！！！" );
                        return;
                    }
                } else {
                    System.out.println( "该手机号错误..." );
                    return;
                }
            }
        }
    }

    public static void showcategory() throws Exception {
        List<Element> bean = new SAXReader().read( "E:\\untitled5\\src\\rc\\demo12_13\\category.xml" ).getRootElement().elements( "bean" );
        System.out.println( "商品类别id\t商品类别名称" );
        for (Element element : bean) {
            System.out.println( element.attributeValue( "id" ) + "\t\t\t" + element.attributeValue( "cname" ) );
        }
    }

    public static void showproduct() throws Exception {
        List<Element> bean = new SAXReader().read( "E:\\untitled5\\src\\rc\\demo12_13\\product.xml" ).getRootElement().elements( "bean" );
        System.out.println( "商品类别id\t商品名称" );
        for (Element element : bean) {
            System.out.println( element.attributeValue( "id" ) + "\t\t\t" + element.attributeValue( "pname" ) );
        }
    }

    public static void showproductDetails() throws Exception {
        Scanner scanner = new Scanner( System.in );
        System.out.println( "请输入你要查看的商品类别ID\n【手机】：1\t\t【电脑】：2" );
        String s = scanner.nextLine();
        List<Element> bean = new SAXReader().read( "E:\\untitled5\\src\\rc\\demo12_13\\product.xml" ).getRootElement().elements( "bean" );
        System.out.println( "商品类别id\t商品名称" );
        for (Element element : bean) {
            if (element.attributeValue( "cid" ).equals( s )) {
                System.out.println( element.attributeValue( "id" ) + "\t\t\t" + element.attributeValue( "pname" ) );
            }
        }
        System.out.println( "请输入输入接下来的操作：1【退出】，2【根据商品id查看商品详情信息】" );
        String s1 = scanner.nextLine();
        switch (s1) {
            case "1":
                return;
            case "2":
                List<Element> bean1 = new SAXReader().read( "E:\\untitled5\\src\\rc\\demo12_13\\productDetails.xml" ).getRootElement().elements( "bean" );
                showproduct();
                System.out.println( "请输入你想查看的商品id" );
                String s2 = scanner.nextLine();
                for (Element element : bean) {
                    for (Element element1 : bean1) {
                        if (element.attributeValue( "id" ).equals( s2 ) && element1.attributeValue( "productId" ).equals( s2 )) {
                            System.out.println( element.attributeValue( "pname" ) + "\t" + element1.attributeValue( "color" ) + "\t" + element1.attributeValue( "id" ) + "\t" + element1.attributeValue( "memory" ) + "\t" + element1.attributeValue( "num" ) + "\t" + element1.attributeValue( "price" )
                            );
                        }
                    }
                }
                break;
        }
    }

    public static void buy() throws Exception {
        if (logrn() == false) {
            System.out.println( "用户请登录..." );
            return;
        } else {
            long time1 = new Date().getTime();
        }
    }

    public static void cats() throws Exception {

    }
}